import { Controller, Get, Put, Param, Body, Req, UnauthorizedException } from '@nestjs/common';
import { UserService } from './user.service';
import { UpdateUserDto, UserResponseDto } from '../../dto/user.dto';
import type { Request } from 'express';
import { User } from '../../entities/user.entity';
import { ApiTags, ApiOperation, ApiResponse, ApiBody, ApiParam } from '@nestjs/swagger';

@ApiTags('用户管理')
@Controller('api/users')
export class UserUserController {
  constructor(private readonly userService: UserService) {}

  @Get('me')
  @ApiOperation({
    summary: '获取当前用户信息',
    description: '根据登录状态获取当前用户的详细信息',
  })
  @ApiResponse({
    status: 200,
    description: '获取成功',
    type: User,
  })
  @ApiResponse({
    status: 401,
    description: '用户未登录',
  })
  getProfile(@Req() req: Request): Promise<User> {
    const user = (req as any).user as User;
    if (!user) {
      throw new UnauthorizedException('用户未登录');
    }
    return this.userService.getProfile(user.id);
  }

  @Put('me')
  @ApiOperation({
    summary: '更新当前用户信息',
    description: '更新当前登录用户的个人信息',
  })
  @ApiBody({
    type: UpdateUserDto,
    description: '更新的用户信息',
  })
  @ApiResponse({
    status: 200,
    description: '更新成功',
    type: UserResponseDto,
  })
  @ApiResponse({
    status: 401,
    description: '用户未登录',
  })
  updateCurrentUser(
    @Body() updateUserDto: UpdateUserDto,
    @Req() req: Request
  ): Promise<UserResponseDto> {
    const user = (req as any).user as User;
    if (!user) {
      throw new UnauthorizedException('用户未登录');
    }
    return this.userService.update(user.id, updateUserDto);
  }
}